import tkinter as tk
from tkinter import simpledialog, messagebox
from datetime import datetime
from logic.task_function import save_task

def is_overdue(due_date_str):
    due_date = datetime.strptime(due_date_str, '%Y-%m-%d %H:%M:%S')
    return due_date < datetime.now()

def on_click_button_task_append(dict, listbox):
    def save_task_callback():
        task_name = entry_task_name.get()
        task_due_date = entry_task_due_date.get()
        task_description = entry_task_description.get()
        task_priority = priority_var.get()
        
        if not task_name or not task_due_date or not task_description:
            messagebox.showwarning("Input Error", "All fields are required.")
            return
        
        try:
            due_date = datetime.strptime(task_due_date, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            messagebox.showwarning("Input Error", "Invalid date format. Please use YYYY-MM-DD HH:MM:SS.")
            return
        
        save_task(dict, task_name, task_due_date, task_description, 'information/task_information.json', task_priority, listbox)
        append_window.destroy()

    append_window = tk.Toplevel()
    append_window.config(bg='#FFFFE0')
    append_window.geometry('400x300')
    append_window.title('Append Your Task')

    label_text1 = tk.Label(append_window, text='输入你的任务/write your task information')
    label_text1.config(bg='#F5F5F5')
    label_text1.pack()

    label_text_edit = tk.Label(append_window, text='输入任务详细/please put on u detail')
    label_text_edit.config(bg='#800080')
    label_text_edit.pack()

    label_text_name = tk.Label(append_window, text='任务名/name:')
    label_text_name.pack()
    entry_task_name = tk.Entry(append_window)
    entry_task_name.pack()

    label_text_due_date = tk.Label(append_window, text='截止日期/due date (YYYY-MM-DD HH:MM:SS):')
    label_text_due_date.pack()
    entry_task_due_date = tk.Entry(append_window)
    entry_task_due_date.pack()

    label_text_description = tk.Label(append_window, text='任务描述/description:')
    label_text_description.pack()
    entry_task_description = tk.Entry(append_window)
    entry_task_description.pack()

    label_text2 = tk.Label(append_window, text='选择优先级/choose the priority')
    label_text2.pack()

    priority_var = tk.StringVar(value='medium')
    priority_options = ['high', 'medium', 'low']
    priority_menu = tk.OptionMenu(append_window, priority_var, *priority_options)
    priority_menu.pack()

    button_save_task = tk.Button(append_window, text='Save', command=save_task_callback)
    button_save_task.pack()

def on_click_button_task_edit(dict, listbox, task_key, task_name, task_priority, task_due_date):
    def save_task_callback():
        new_task_name = entry_task_name.get()
        new_task_due_date = entry_task_due_date.get()
        new_task_description = entry_task_description.get()
        new_task_priority = priority_var.get()

        if not new_task_name or not new_task_due_date or not new_task_description:
            messagebox.showwarning("Input Error", "All fields are required.")
            return

        try:
            due_date = datetime.strptime(new_task_due_date, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            messagebox.showwarning("Input Error", "Invalid date format. Please use YYYY-MM-DD HH:MM:SS.")
            return

        dict[task_key]['task_name'] = new_task_name
        dict[task_key]['task_priority'] = new_task_priority
        dict[task_key]['task_due_date'] = new_task_due_date

        selected_index = listbox.curselection()
        listbox.delete(selected_index)
        listbox.insert(selected_index, f"{new_task_name} priority:{new_task_priority} due to {new_task_due_date}")

        save_task(dict, new_task_name, new_task_due_date, new_task_description, 'information/task_information.json', new_task_priority, listbox)
        edit_window.destroy()

    edit_window = tk.Toplevel()
    edit_window.config(bg='#FFFFE0')
    edit_window.geometry('400x300')
    edit_window.title('Edit Task')

    label_text1 = tk.Label(edit_window, text='编辑任务/edit your task information')
    label_text1.config(bg='#F5F5F5')
    label_text1.pack()

    label_text_name = tk.Label(edit_window, text='任务名/name:')
    label_text_name.pack()
    entry_task_name = tk.Entry(edit_window)
    entry_task_name.pack()
    entry_task_name.insert(0, task_name)

    label_text_due_date = tk.Label(edit_window, text='截止日期/due date (YYYY-MM-DD HH:MM:SS):')
    label_text_due_date.pack()
    entry_task_due_date = tk.Entry(edit_window)
    entry_task_due_date.pack()
    entry_task_due_date.insert(0, task_due_date)

    label_text_description = tk.Label(edit_window, text='任务描述/description:')
    label_text_description.pack()
    entry_task_description = tk.Entry(edit_window)
    entry_task_description.pack()
    entry_task_description.insert(0, dict[task_key]['task_description'])

    label_text2 = tk.Label(edit_window, text='选择优先级/choose the priority')
    label_text2.pack()

    priority_var = tk.StringVar(value=task_priority)
    priority_options = ['high', 'medium', 'low']
    priority_menu = tk.OptionMenu(edit_window, priority_var, *priority_options)
    priority_menu.pack()

    button_save_task = tk.Button(edit_window, text='Save', command=save_task_callback)
    button_save_task.pack()